<template>
  <div class="login" :style="'background-image:url('+ Background +');'">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left" label-width="0px" class="login-form">
      <h3 class="title">
        问易得法律咨询
      </h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="手机号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="code">
        <el-input v-model="loginForm.code" auto-complete="off" placeholder="验证码" style="width: 63%" @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
        </el-input>
        <div class="login-code">
          <el-button :disabled="disabled" class="codeBtn" @click="getCode">{{ smsText }}</el-button>
        </div>
      </el-form-item>
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="medium" type="primary" style="width:100%;" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <div v-if="$store.state.settings.showFooter" id="el-login-footer">
      <span v-html="$store.state.settings.footerTxt" />
      <span> ⋅ </span>
      <a href="https://beian.miit.gov.cn/#/Integrated/index" target="_blank">{{ $store.state.settings.caseNumber }}</a>
    </div>
  </div>
</template>

<script>
import { getPhoneCode, getInfo } from '@/api/login'
import Cookies from 'js-cookie'
import qs from 'qs'
import Background from '@/assets/images/background.jpeg'
export default {
  name: 'PhoneLogin',
  data() {
    return {
      Background: Background,
      codeUrl: '',
      cookiePass: '',
      loginForm: {
        username: '',
        code: '',
        uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '手机号不能为空' }],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      loading: false,
      redirect: undefined,
      smsText: '获取验证码',
      disabled: false
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        const data = route.query
        if (data && data.redirect) {
          this.redirect = data.redirect
          delete data.redirect
          if (JSON.stringify(data) !== '{}') {
            this.redirect = this.redirect + '&' + qs.stringify(data, { indices: false })
          }
        }
      },
      immediate: true
    }
  },
  created() {
    // 获取用户名密码等Cookie
    this.getCookie()
    // token 过期提示
    this.point()
    console.log(this.$route.query.pid)
  },
  methods: {
    getCode() {
      if (this.loginForm.username.length !== 11) {
        this.$notify({
          title: '提示',
          message: '请输入正确的手机号码',
          type: 'warning',
          duration: 2000
        })
        return
      }
      getPhoneCode(this.loginForm.username).then(res => {
        this.loginForm.uuid = res.uuid
        this.disabled = true
        this.loopSecond(120)
      })
    },
    loopSecond(second) {
      this.smsText = second + '秒后获取'
      const handler = setInterval(() => {
        if (second > 0) {
          this.smsText = second + '秒后获取'
          --second
        } else {
          this.smsText = '获取验证码'
          this.disabled = false
          clearInterval(handler)
        }
      }, 1000)
    },
    getCookie() {
      const username = Cookies.get('username')
      this.loginForm = {
        username: username === undefined ? this.loginForm.username : username,
        code: ''
      }
    },
    handleLogin() {
      const user = {
        username: this.loginForm.username,
        code: this.loginForm.code,
        uuid: this.loginForm.uuid,
        pid: this.$route.query.pid
      }
      this.loading = true
      this.$store.dispatch('PhoneLogin', user).then(() => {
        this.loading = false
        getInfo().then(res => {
          if (res.user.role.level === 1) {
            this.$router.push({ path: this.redirect || '/' })
          } else {
            if (this.redirect === '/dashboard') {
              this.$router.push({ path: '/user/center' })
            } else {
              this.$router.push({ path: this.redirect || '/' })
            }
          }
        }).catch(() => {

        })
      }).catch(() => {
        this.loading = false
        // this.getCode()
      })
    },
    point() {
      const point = Cookies.get('point') !== undefined
      if (point) {
        this.$notify({
          title: '提示',
          message: '当前登录状态已过期，请重新登录！',
          type: 'warning',
          duration: 5000
        })
        Cookies.remove('point')
      }
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
  .login {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100%;
    background-size: cover;
  }
  .title {
    margin: 0 auto 30px auto;
    text-align: center;
    color: #707070;
  }

  .login-form {
    border-radius: 6px;
    background: #ffffff;
    width: 385px;
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon{
      height: 39px;width: 14px;margin-left: 2px;
    }
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    display: inline-block;
    height: 38px;
    float: right;
    img{
      cursor: pointer;
      vertical-align:middle
    }
  }
</style>
